package com.zycfc.zsf.boot.mq.rabbitmq.util;

import java.io.*;
import org.slf4j.*;

public class FileUtil
{
    private static final Logger log;
    
    public static void writeFile(final String path, final byte[] bytes, final boolean append) throws Exception {
        if (null == path || path.length() == 0 || bytes == null) {
            return;
        }
        final File file = new File(path);
        if (!file.exists()) {
            final File parent = file.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
        }
        try (final OutputStream outputStream = new FileOutputStream(file, append)) {
            outputStream.write(bytes);
            outputStream.flush();
        }
        catch (Exception e) {
            FileUtil.log.error(e.getMessage(), (Throwable)e);
        }
    }
    
    public static byte[] readFile(final String path) {
        if (null == path || path.length() == 0) {
            return null;
        }
        final File file = new File(path);
        if (!file.exists()) {
            return null;
        }
        try (final InputStream inputStream = new FileInputStream(file)) {
            int len = 0;
            final byte[] buf = new byte[1024];
            final ByteArrayOutputStream bStream = new ByteArrayOutputStream();
            while ((len = inputStream.read(buf)) != -1) {
                bStream.write(buf, 0, len);
            }
            return bStream.toByteArray();
        }
        catch (Exception e) {
            FileUtil.log.error(e.getMessage(), (Throwable)e);
            return null;
        }
    }
    
    static {
        log = LoggerFactory.getLogger((Class)FileUtil.class);
    }
}
